package com.example.dreamlist2.todo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dreamlist2.R;
import com.example.dreamlist2.User;
import com.example.dreamlist2.Utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.datatype.BmobDate;

public class TodoAdapter extends BaseAdapter {
    private List<TodoItem> list;
    private Context context;

    public TodoAdapter(Context context, List<TodoItem> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public TodoItem getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            //设置该条的文字展示
            String type, title, content, date_time;
            TodoItem todo = getItem(position);
            BmobDate date = todo.getDate();
            date_time = date.getDate();
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            date_time = sdf.format(date);s
            type = todo.getType();
            title = todo.getTitle();
            content = todo.getContent();
            //LayoutInflater inflater = LayoutInflater.from(context);
            @SuppressLint("ViewHolder") View view = LayoutInflater.from(context).inflate(R.layout.todo_item, parent,false);
            //convertView = inflater.inflate(R.layout.todo_item,  null);//实例化一个布局文件
            TextView ttype=view.findViewById(R.id.type);
            TextView ttitle = view.findViewById(R.id.title);
            TextView tcontent = view.findViewById(R.id.content);
            TextView tdate = view.findViewById(R.id.datetime);
            ttype.setText("类别-"+type);
            ttitle.setText(title);
            tcontent.setText(content);
            tdate.setText(date_time);

            //根据是否完成设置图片
            Integer flag = getItem(position).getF_done();
            ImageView done = view.findViewById(R.id.status);
            if(flag == 1){
                done.setImageResource(R.drawable.done);
                //done.setImageResource(Utils.getimages("done.png"));
            }
            else{
                done.setImageResource(R.drawable.due);
                //done.setImageResource(Utils.getimages("due.png"));
            }
            return view;
    }
}
